package com.xiaoma.guicedemo.test;

/**
 * @author xiaoma
 * @date 2020-04-27 16:38
 */
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.name.Named;
import com.xiaoma.guicedemo.communicator.Communicator;
import com.xiaoma.guicedemo.module.BasicModule;
import com.xiaoma.guicedemo.service.MyService;


public class CommunicationMain {

    @Inject
    //Communicator有多个实现类，使用@Named来区分
    @Named("communicator")
    private Communicator communicator;

    @Inject
    //anotherCommunicator要和BasicModule中对应
    @Named("anotherCommunicator")
    private Communicator anotherCommunicator;

    public CommunicationMain(Boolean keepRecords) {
        if (keepRecords) {
            System.out.println("Message logging enabled");
        }
    }

    public boolean sendMessage(String message) {
        communicator.sendMessage(message);
        anotherCommunicator.sendMessage(message);
        return true;
    }

    public static void main(String[] args) {
         // Injector注射器，为依赖的管理上下文
        Injector injector = Guice.createInjector(new BasicModule());
        CommunicationMain comms = injector.getInstance(CommunicationMain.class);
        comms.sendMessage("hello world");
        MyService instance = injector.getInstance(MyService.class);
        instance.test("sdfsd");

/*
        Map<Key<?>, Binding<?>> bindings = injector.getBindings();
        for (Map.Entry<Key<?>, Binding<?>> bingingEntry : bindings.entrySet()) {
            Binding binging = bingingEntry.getValue();
            Key key =  binging.getKey();
            Provider provider = binging.getProvider();
            System.out.println("Key: " + key.toString());
            System.out.println("Provider: " + provider.get().getClass());
            System.out.println("************");
        }*/
    }
}
